<template>
	<view>
		<view class="page" v-if="isShow">
			<custom-header title="平台公告"></custom-header>

			<!-- 列表 -->
			<scroll-view scroll-y enable-back-to-top @scrolltolower="loadMore" @refresherrefresh="onRefresh"
				refresher-enabled :refresher-triggered="isRefreshing" style="height: 100%; padding: 20rpx 24rpx;">
				<block v-for="(item, index) in announcementList" :key="index">
					<view class="list-item" @click="toDetail(item.id)">
						<view class="item-header">
							<image style="height: 32rpx; width: 36rpx; margin-right: 20rpx;"
								src="../../static/images/ic_notice_item.png"></image>
							<view style="font-size: 28rpx; color: rgba(0, 0, 0, 0.60); flex: 1;">平台公告</view>
							<view style="font-size: 20rpx; color: rgba(0, 0, 0, 0.40);">提交时间：{{item.createTime}}</view>
						</view>
						<view style="height: 2rpx; background-color: #EAEAEA; margin: 22rpx 0;"></view>
						<view class="item-info">
							<view>{{ item.title }}</view>
						</view>

						<view class="item-content" v-html="item.content"></view>
					</view>
				</block>
				<view v-if="loadingMore" class="loading-more">加载中...</view>
				<view v-if="noMoreData" class="no-more-data">已经全部加载完毕</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				announcementList: [],
				page: 1,
				pageSize: 10,
				isRefreshing: false, //正在刷新
				loadingMore: false, //加载中
				noMoreData: false, //没有更多
				isEnd: false, // 是否全部加载完成
				total: 0, //总数据量
				isShow: uni.getStorageSync("isShow"),
			};
		},
		onLoad() {
			this.fetchAnnouncements(0)
		},
		methods: {
			onRefresh() {
				this.isRefreshing = true;
				this.resetPagination();
				this.fetchRecord(1);
			},
			// 清空状态
			resetPagination() {
				this.page = 1;
				this.announcementList = [];
				this.loadingMore = false;
				this.noMoreData = false;
			},
			loadMore() {
				if (!this.loadingMore && !this.noMoreData) {
					this.loadingMore = true;
					this.page += 1;
					this.fetchAnnouncements(2)
				}
			},
			async fetchAnnouncements(states) {
				const res = await this.$http({
					method: 'GET',
					url: '/article/notice/list',
					data: {
						pageSize: this.pageSize,
						pageNun: this.page,
					}
				});

				if (res) {
					//状态修改
					if (states == 1) {
						this.isRefreshing = false
					} else if (states == 2) {
						this.loadingMore = false;
					}

					// 首次加载
					if (this.page === 1) {
						this.announcementList = res.rows
						this.total = res.total
					} else {
						// 追加数据
						this.announcementList = [...this.announcementList, ...res.rows]
					}

					// 判断是否加载完成
					this.isEnd = (this.page * this.pageSize) >= res.total

					// 翻页或加载到最后了
					if (!this.isEnd) {
						this.page++;
					} else {
						this.noMoreData = true;
					}
				} else {
					if (states == 1) {
						this.isRefreshing = false
					} else if (states == 2) {
						this.loadingMore = false;
					}
				}
			},
			toDetail(id) {
				uni.navigateTo({
					url: `/pages/detail/detail?id=${id}`
				})
			}
		}
	};
</script>

<style lang="scss" scoped>
	.page {
		height: 100vh;
		background-color: #F7FAFF;
	}

	.list-item {
		border-radius: 12rpx;
		background-color: white;
		padding: 20rpx 46rpx;
		margin-bottom: 20rpx;
	}

	.item-header {
		margin-bottom: 5px;
		display: flex;
		align-items: center;
	}

	.item-info {
		margin-bottom: 24px;
		font-size: 28rpx;
		color: black;
		font-weight: bold;
	}

	.item-content {
		font-size: 28rpx;
		color: rgba(0, 0, 0, 0.60);
		text-overflow: -o-ellipsis-lastline;
		overflow: hidden; //溢出内容隐藏
		text-overflow: ellipsis; //文本溢出部分用省略号表示
		display: -webkit-box; //特别显示模式
		-webkit-line-clamp: 4; //行数
		line-clamp: 4;
		-webkit-box-orient: vertical; //盒子中内容竖直排列
	}

	// 加载更多
	.loading-more,
	.no-more-data {
		text-align: center;
		padding: 10px;
	}
</style>